<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button class="search-btn" slot="title" type="info" size="small" round icon="search" to="/search">搜索
      </van-button>
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- tabbar -->
    <van-tabs v-model="active" animated swipeable class="channel-tabs">
      <van-tab :title='i.name' v-for='i in channels' :key='i.id'>
        <!-- 文章列表 -->
        <articleList :channelId='i.id' />
      </van-tab>
      <div slot="nav-right" class="placeholder">
      </div>
      <div slot='nav-right' class="hamburger-btn" @click="show=true">
        <i class='icon icon-gengduo'></i>
      </div>
    </van-tabs>
    <!-- /tabbar -->
    <van-popup class="edit-channel-popup" v-model="show" position="bottom" :style="{ height: '100%' }" closeable
      close-icon-position='top-left'>
      <channelEdit :channels="channels" :active="active" :allChannels='allChannels' @ChannelState='ChannelDome' />
    </van-popup>
  </div>
</template>

<script>
import channelEdit from './component/channel-edit.vue'
import { channelsList, getAllChannels } from '@/api/channels.js'
import articleList from './component/article-list.vue'
import { getItem } from '@/utils/seccession.js'
export default {
  name: 'HomeIndex',
  components: { articleList, channelEdit },
  props: {},
  data () {
    return {
      active: 0,
      // 用户频道列表
      channels: [],
      show: false,
      allChannels: []
    }
  },
  computed: {},
  watch: {},
  created () {
    this.channelList()
    this.allChannelsList()
  },
  mounted () { },
  methods: {
    ChannelDome (state, i) {
      this.show = state
      this.active = i
    },
    // 获取频道列表
    async channelList () {
      if (!this.$store.state.user && getItem('hmtt_channel')) {
        this.channels = getItem('hmtt_channel')
      } else {
        try {
          const res = await channelsList()
          this.channels = res.data.data.channels
        } catch (err) {
          this.$toast('获取频道列表数据失败')
        }
      }
    },
    async allChannelsList () {
      const { data: { data } } = await getAllChannels()
      this.allChannels = data.channels
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-bottom: 100px;
  .edit-channel-popup {
    padding-top: 100px;
    box-sizing: border-box;
  }
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    margin-top: 180px;
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 2;
      width: 100%;
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      i.icon {
        font-size: 33px;
      }
      &::before {
        content: '';
        position: absolute;
        left: 0;
        width: 2px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      height: 80px;
      .van-tab__text {
        font-size: 27px;
        color: #777777;
      }
    }

    .van-tab--active .van-tab__text {
      color: #333 !important;
    }
    .van-tabs__line {
      width: 31px !important;
      height: 6px;
      background: rgba(50, 150, 250, 1);
      border-radius: 3px;
      bottom: 8px;
    }
    .van-tabs__nav {
      padding: 0;
    }
  }
}
</style>
